package gr.uoa.di.thanos.botcraft.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import org.lwjgl.LWJGLException;

import gr.uoa.di.thanos.botcraft.Configuration;

public class Launcher extends JFrame implements ActionListener, Runnable {
	private static final long serialVersionUID = 1L;
	private static final String BACKGROUND = "/botcraft.png";
	private static final Logger LOGGER = Logger.getLogger(Launcher.class.getName());

	private final Configuration configuration;
	private final JButton newGame;
	private final JButton settings;
	private final JButton about;
	private final JButton exit;
	
	public static void main(final String[] arguments) {
		try {
			SwingUtilities.invokeAndWait(new Launcher(new Configuration()));
		} catch (final InterruptedException e) {
		} catch (final InvocationTargetException e) {
			throw new RuntimeException(e.getCause());
		}
	}
	
	public Launcher(final Configuration configuration) {
		this.configuration = configuration;
		setTitle(configuration.getResourceBundle().getString("botcraftLauncher"));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		try {
			final Image background = ImageIO.read(Launcher.class.getResource(BACKGROUND));
			final JPanel contentPane = new JPanel() {
				private static final long serialVersionUID = 0L;
	
				@Override
				protected void paintComponent(final Graphics graphics) {
					graphics.drawImage(background, 0, 0, Color.BLACK, null);
				}
			};
			final Dimension size = new Dimension(background.getWidth(null), background.getHeight(null));
			contentPane.setMinimumSize(size);
			contentPane.setMaximumSize(size);
			contentPane.setPreferredSize(size);
			setContentPane(contentPane);
			setResizable(false);
		} catch (final IOException e) {
			LOGGER.log(Level.WARNING, "Error loading background image", e);
		}
		setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		newGame = new JButton(configuration.getResourceBundle().getString("newGame"));
		newGame.setAlignmentX(Component.CENTER_ALIGNMENT);
		newGame.addActionListener(this);
		add(newGame);
		settings = new JButton(configuration.getResourceBundle().getString("settings"));
		settings.setAlignmentX(Component.CENTER_ALIGNMENT);
		settings.addActionListener(this);
		add(settings);
		about = new JButton(configuration.getResourceBundle().getString("about"));
		about.setAlignmentX(Component.CENTER_ALIGNMENT);
		about.addActionListener(this);
		add(about);
		exit = new JButton(configuration.getResourceBundle().getString("exit"));
		exit.setAlignmentX(Component.CENTER_ALIGNMENT);
		exit.addActionListener(this);
		add(exit);
		pack();
	}

	@Override
	public void actionPerformed(final ActionEvent actionEvent) {
		if (actionEvent.getSource() == newGame) {
			try {
				SwingUtilities.invokeLater(new GuiClient(configuration));
			} catch (final LWJGLException e) {
				LOGGER.log(Level.WARNING, "Error initializing OpenGL", e);
				JOptionPane.showMessageDialog(this, configuration.getResourceBundle().getString("errorInitializingOpenGL"), e.getClass().getName(), JOptionPane.WARNING_MESSAGE);
			}
		} else if (actionEvent.getSource() == settings) {
			SwingUtilities.invokeLater(new Settings(configuration));
		} else if (actionEvent.getSource() == about) {
			try {
				SwingUtilities.invokeLater(new About(configuration));
			} catch (final IOException e) {
				LOGGER.log(Level.WARNING, "Error loading about", e);
				JOptionPane.showMessageDialog(this, configuration.getResourceBundle().getString("errorLoadingAbout"), e.getClass().getName(), JOptionPane.WARNING_MESSAGE);
			}
		} else if (actionEvent.getSource() == exit)
			dispose();
	}
	
	@Override
	public void run() {
		setVisible(true);
		newGame.requestFocusInWindow();
	}
}
